package com.company;

/**
 * Created by elimydlarz on 14/04/14.
 */
public class ShapeWriter {

    private final String LINE_BREAK = System.getProperty("line.separator");

    private Boolean isOdd(Integer n) {
        if (n % 2 == 0) return false;
        else return true;
    }

    public void writeRightAngleTriangle(Integer length) {
        StringBuilder triangle = new StringBuilder();
        for (int i = 0; i < length; i++) {
            for (int j = 0; j <= i; j++) {
                triangle.append('*');
            }
            triangle.append(LINE_BREAK);
        }
        System.out.print(triangle);
    }

    public void writeCenteredTriangle(Integer centreWidth) {
        if (isOdd(centreWidth)) {

            StringBuilder triangle = new StringBuilder();

            Integer baseWidth = (centreWidth * 2) - 1;
            Integer height = centreWidth;
            Integer horizontalCenterIndex = centreWidth - 1;

            for (int row = 0; row < height; row++) {

                for (int column = 0; column < baseWidth; column++) {

                    if (column < horizontalCenterIndex - row || column > horizontalCenterIndex + row) {
                        triangle.append(' ');
                    }
                    else triangle.append('*');

                }
                triangle.append(LINE_BREAK);

            }
            System.out.print(triangle);

        } else {
            System.out.println("Centered triangles must have an odd-numbered centre width.");
        }
    }

    public void writeCenteredDiamond(Integer centreWidth) {
        if (isOdd(centreWidth)) {

            writeCenteredTriangle(centreWidth);
            StringBuilder bottomTriangle = new StringBuilder();

            Integer baseWidth = (centreWidth * 2) - 1;
            Integer horizontalCenterIndex = centreWidth - 1;
            Integer verticalCenterIndex = centreWidth - 1;

            for (int row = 0; row < verticalCenterIndex; row++) {
                bottomTriangle.insert(0, LINE_BREAK);

                for (int column = 0; column < baseWidth; column++) {

                    if (column < horizontalCenterIndex - row || column > horizontalCenterIndex + row) {
                        bottomTriangle.insert(0, ' ');
                    }
                    else bottomTriangle.insert(0, '*');

                }

            }
            System.out.print(bottomTriangle);

        }
        else {
            System.out.println("Centered diamonds must have an odd-numbered centre width.");
        }
    }

}
